<?php
// 应用公共文件

if (!function_exists('__url')) {

    /**
     * 构建URL地址
     * @param string $url
     * @param array $vars
     * @param bool $suffix
     * @param bool $domain
     * @return string
     */
    function __url(string $url = '', array $vars = [], $suffix = true, $domain = false)
    {
        return url($url, $vars, $suffix, $domain)->build();
    }
}
/**
 * 返回数据
 */
if (!function_exists('json_to_respos')) {
    function json_to_respos($msg='',$code=200,$data=[])
    {
        if(empty($data)){
            return json(['msg'=>$msg,'code'=>$code]);
        }else{
            return json(['msg'=>$msg,'code'=>$code,'data'=>$data]);
        }

    }
}
/**
 * 字符串加密
 */
if (!function_exists('password_encode')) {
    function password_encode($str,$haxi=true)
    {
        $str = trim($str);
        if($haxi){
            return password_hash($str,PASSWORD_DEFAULT);
        }else{
            return md5(md5($str));
        }
    }
}
/**
 * 验证加密
 */
if (!function_exists('password_decode')) {
    function password_decode($str,$passworder,$haxi=true)
    {
        $str = trim($str);
        if($haxi){
            return password_verify($str,$passworder);
        }else{
            if(md5(md5($str))==$passworder){
                return true;
            }else{
                return false;
            }
        }
    }
}
/**
 * 单独验证数据
 */
if (!function_exists('Validate')) {
    function Validate($data,$array)
    {
        $validate =\think\facade\Validate::rule($array);
        if(!$validate->check($data)){
            $val['code'] = false;
            $val['msg'] = $validate->getError();
        }else{
            $val['code'] = true;
            $val['msg'] = '验证成功';
        }
        return [$val['code'],$val['msg']];
    }
}
/**
 * layui返回数据
 */
if (!function_exists('json_page_response')) {
    function json_page_response($data){
        if(!isset($data['total'])){
            return json(['code'=>0,'msg'=>'获取成功','count'=>1,'data'=>$data['data']]);
        }
        return json(['code'=>0,'msg'=>'获取成功','count'=>$data['total'],'data'=>$data['data']]);
    }
}
/**
 * layui返回数据
 */
if (!function_exists('is_assoc')) {
    function is_assoc($arr){
        return array_keys($arr) !== range(0, count($arr) - 1);
    }
}
if (!function_exists('getlange')) {
    function getlange($value){
        return $value;
    }
}
/**
 * layui获取图片地址
 */
if (!function_exists('getFilePath')) {
    function getFilePath($id,$isarray=false,$quan=false,$defult=null){
//        echo $id;exit;
        $id = strval($id);
        $data = \think\facade\Db::name('enclosure')
            ->where('id','in',$id)
            ->select()->toArray();
//        echo '<pre>';
//        print_r($data);
//        exit;
        if($data){
            $img = [];
            foreach ($data as $value){
                if(!strstr($value['path'],'http')){
                    $value['path'] = str_replace('\\','/',$value['path']);
                }else{
                    $value['path'] = str_replace('\\','/',$value['quan_path']);
                }
                if($quan){
                    $img[] = $value;
                }else{
                    $img[] = $value['path'];
                }
            }
            if($isarray){
                return $img;
            }else{
                return $img[0];
            }
        }else{
            if($defult){
                return $defult;
            }else{
                return '/static/error.jpg';
            }
        }
    }
}
/**
 * app获取图片地址
 */
if(!function_exists('AppGetFilePath')){
    function AppGetFilePath($id,$isarray=false,$quan=false,$defult=null){
        $str = getFilePath($id,$isarray,$quan,$defult);
        if($isarray){
            $newarray = [];
            foreach ($str as $v){
                if(!strstr($v,'http')){
                    $newarray[] = request()->domain().$v;
                }else{
                    $newarray[] = $v;
                }
            }
            return $newarray;
        }else{
            if(!strstr($str,'http')){
                $str = request()->domain().$str;
            }
            return $str;
        }
    }
}
/**
 * 获取配置
 */
if(!function_exists('getSysTem')){
    function getSysTem($str,$field='value',$is_array=false){
        $array = explode('.',$str);
        $pid = 0;
        $data = '';
//        echo '<pre>';
//        print_r($array);
        foreach ($array as $value){
            $data = \think\facade\Db::name('systems')
                ->where('pid',$pid)
                ->where('name',$value)
                ->findOrEmpty();
//            if(empty($data)){
//                return false;
//            }
//            print_r($data);
//            exit;
            $pid = $data['id'];
        }
//        echo '<pre>';
//        print_r($data);
//        exit;
        if(isset($data['tupian'])){
            if($data['tupian']=='单文件'||$data['tupian']=='单图片'){
                if($data['value']){
                    $data['value'] = getFilePath($data['value']);
                }
            }elseif ($data['tupian']=='多文件'||$data['tupian']=='多图片'){
                if($data['value']){
                    $data['value'] = getFilePath($data['value'],true);
                }
            }
        }
        if($is_array){
            return $data??'';
        }else{
            return $data[$field]??'';
        }
    }
}
if(!function_exists('myurl')){
    function myurl($str){
        $str_array = explode('?',$str);
        $newstr = '';
        if(isset($str_array[1])&&$str_array[1]){
            $newstrarray = explode('&',$str_array[1]);
            foreach ($newstrarray as $value){
                if($value){
                    $news = explode('=',$value);
                    $newstr .= $news[0].'/'.$news[1].'/';
                }
            }
        }
        if(empty($newstr)){
            $str = url($str_array[0])->build();
        }else{
            $newstr = trim($newstr,'/');
            $qianmian_array = explode('/',$str_array[0]);
            if(count($qianmian_array)==1){
                $str ='/'.request()->app().'/'.app('request')->controller().'/'.$str_array[0].'/'.$newstr;
                $str = url($str)->build();
            }elseif (count($qianmian_array)==2){
                $str ='/'.request()->app().'/'.$str_array[0].'/'.$newstr;
                $str = url($str)->build();
            }else{
                $str =$str_array[0].'/'.$newstr;
                $str = url($str)->build();
            }
        }
        return $str;
    }
}
if(!function_exists('get_ip')){
    function get_ip()

    {

        $ip=FALSE;

        //客户端IP 或 NONE

        if(!empty($_SERVER["HTTP_CLIENT_IP"])){

            $ip = $_SERVER["HTTP_CLIENT_IP"];

        }

        //多重代理服务器下的客户端真实IP地址（可能伪造）,如果没有使用代理，此字段为空

        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);

            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }

            for ($i = 0; $i < count($ips); $i++) {

                if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {

                    $ip = $ips[$i];

                    break;

                }

            }

        }

        //客户端IP 或 (最后一个)代理服务器 IP

        return ($ip ? $ip : ($_SERVER['REMOTE_ADDR']??''));

    }
}
/**
 * 获取目录文件名和目录名
 */
if(!function_exists('get_dir_file')){
    function get_dir_file($path,$type='all',$paichu=[])

    {
        $dir = scandir($path);
        $newdir = [];
        foreach ($dir as $value){
            if($value!='.'&&$value!='..'){
                $file_name = $path.$value;
                if($type=='file'){
                    if(is_file($file_name)&&!in_array($value,$paichu)){
                        $newdir[] = $value;
                    }
                }elseif ($type=='dir'&&!in_array($value,$paichu)){
                    if(is_dir($file_name)){
                        $newdir[] = $value;
                    }
                }elseif ($type=='all'&&!in_array($value,$paichu)){
                    $newdir[] = $value;
                }
            }
        }
        return $newdir;
    }
}
/**
 * 将无精度的浮点数转成字符串
 */
if(!function_exists('floatToStr')){
    function floatToStr($float,$type='up',$num=2)
    {
        $float = $float*pow(10,$num);
        if($float>intval($float)){
            if($type=='up'){
                $float = (ceil($float))/pow(10,$num);
            }else{
                $float = floor($float)/pow(10,$num);
            }
        }else{
            $float = intval($float)/pow(10,$num);
        }
        return $float;
    }
}
/**
 * 判断运行系统是不是windows
 */
if(!function_exists('isWondows')){
    function isWondows()
    {
        $str = php_uname('s');
        if(strstr($str,'Window')){
            return true;
        }else{
            return false;
        }
    }
}
/**
 * 替换中文字符串
 */
if(!function_exists('replace_cn_str')){
    function replace_cn_str($str,$replace_str,$start=0,$lenth=1)
    {
        $kaisie = 0;
        $newstar = '';
        $strlen = mb_strlen($str);
        for ($i=0;$i<$strlen;$i++){
            $strs = mb_substr($str,$i,1);
            if($i<$start){
                $newstar .= $strs;
            }else{
                $kaisie++;
                if($kaisie==$lenth){
                    $newstar .= $replace_str;
                }elseif ($kaisie>$lenth){
                    $newstar .= $strs;
                }
            }
        }
        return $newstar;
    }
}
/**
 * 替换中文字符串
 */
if(!function_exists('getDataBaseConfig')){
    function getDataBaseConfig($str='')
    {
        if(empty($str)){
            $array = config('database.connections.'.config('database.default'));
        }else{
            $array = config('database.connections.'.config('database.default').'.'.$str);
        }
        return $array;
    }
}
